// stores/dict.ts
import { defineStore } from 'pinia'
import { ref } from 'vue'

export interface DictItem {
  key: string
  value: any
}

export const useDictStore = defineStore('dict', () => {
  const dict = ref<DictItem[]>([])

  // 获取字典
  const getDict = (_key: string | null): any | null => {
    if (!_key) { return null }
    const item = dict.value.find(entry => entry.key === _key)
    return item ? item.value : null
  }

  // 设置字典
  const setDict = (_key: string, value: any): void => {
    if (!_key) { return }
    const existingIndex = dict.value.findIndex(entry => entry.key === _key)
    if (existingIndex !== -1) {
      dict.value[existingIndex].value = value // 更新
    }
    else {
      dict.value.push({ key: _key, value }) // 添加
    }
  }

  // 删除字典
  const removeDict = (_key: string): boolean => {
    const index = dict.value.findIndex(entry => entry.key === _key)
    if (index !== -1) {
      dict.value.splice(index, 1)
      return true
    }
    return false
  }

  // 清空字典
  const cleanDict = (): void => {
    dict.value = []
  }

  // 初始字典（空函数）
  const initDict = (): void => {
    // Add any init logic here if needed
  }

  return {
    dict,
    getDict,
    setDict,
    removeDict,
    cleanDict,
    initDict,
  }
})
